package com.gimi.cloud.jimi.client.controller.moreInfo;

import com.gimi.cloud.jimi.client.service.moreInfo.SocialSecurityService;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.enums.common.RedisKeyEnum;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.commonService.InterfacePlatform.impl.InterfaceUtil;
import com.gimi.cloud.jimi.dao.redis.IRedisService;
import com.gimi.cloud.jimi.dto.params.SocialSecurityParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 蜂巢社保
 *
 * @author zhoufuhang
 * @time 2018年1月9日
 */
@RestController
@RequestMapping("/moreInfo")
public class SocialSecurityController {
    private static Logger logger = LoggerFactory.getLogger(SocialSecurityController.class);
    private final static String redisKey = RedisKeyEnum.JIMI_CONFIG_PRE.getKey() + "interface_platform_universal_socialSecurity";
    private final static String FENGCHAO_SHEBAO_CITY = RedisKeyEnum.CITY_LIST_PRE.getKey() + "fengchao_shebao_city";

    private InterfaceUtil interfaceUtil;
    private IRedisService iRedisService;
    private SocialSecurityService socialSecurityService;

    @Autowired
    public SocialSecurityController(InterfaceUtil interfaceUtil, IRedisService iRedisService
            , SocialSecurityService socialSecurityService) {
        this.interfaceUtil = interfaceUtil;
        this.iRedisService = iRedisService;
        this.socialSecurityService = socialSecurityService;
    }

    /**
     * 获取城市列表
     */
    @RequestMapping("/getSheBaoCityList")
    public void getSheBaoCityList(@RequestBody SocialSecurityParam socialSecurityParam, HttpServletResponse response) {
        String result = iRedisService.get(FENGCHAO_SHEBAO_CITY);
        //如果传入城市名字，返回单个城市json
        JSONObject cityJson = JSONObject.fromObject(result);
        JSONArray data = cityJson.getJSONArray("data");
        //如果不传入城市名字，直接返回列表
        if (StringUtil.isBlank(socialSecurityParam.getCityName())) {
            Util.sendResponseFastJson(JSONArray.fromObject(data), ResponseCodeEnum.SUCCESSED, response);
            return;
        }

        logger.info("遍历蜂巢社保城市列表");
        JSONArray returnJSONArray = new JSONArray();
        for (int i = 0; i < data.size(); i++) {
            JSONObject cityObject=  data.getJSONObject(i);
            if(cityObject.getString("cityName").contains(socialSecurityParam.getCityName())){
                returnJSONArray.add(cityObject);
            }
        }
        Util.sendResponseFastJson(JSONArray.fromObject(returnJSONArray), ResponseCodeEnum.SUCCESSED, response);
    }

    /**
     * 获取登录参数
     */
    @RequestMapping("/getSheBaoCityLoginParamer")
    public void getSheBaoCityLoginParamer(@RequestBody SocialSecurityParam socialSecurityParam, HttpServletResponse response) {
        socialSecurityParam.setRedisKey(redisKey);
        socialSecurityParam.setInterfcaeParamKey("getcitylogin");
        socialSecurityParam.setInterfcaeResultKey("check");
        Util.sendResponseFastJson(interfaceUtil.call(socialSecurityParam), ResponseCodeEnum.SUCCESSED, response);
    }

    /**
     * 获取验证码图片
     */
    @RequestMapping("/getSheBaoCodeImage")
    public void getSheBaoCodeImage(@RequestBody SocialSecurityParam socialSecurityParam, HttpServletResponse response) {
        socialSecurityParam.setRedisKey(redisKey);
        socialSecurityParam.setInterfcaeParamKey("getcodeimage");
        socialSecurityParam.setInterfcaeResultKey("check");
        Util.sendResponseFastJson(interfaceUtil.call(socialSecurityParam), ResponseCodeEnum.SUCCESSED, response);
    }

    /**
     * 登录并抓取社保账号
     *
     * @throws Exception
     */
    @RequestMapping("/getSheBaoLoginAndGrab")
    public void getSheBaoLoginAndGrab(@RequestBody SocialSecurityParam socialSecurityParam, HttpServletResponse response) throws Exception {
        Response resp = socialSecurityService.getSheBaoLoginAndGrab(socialSecurityParam);
        Util.sendResponse(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(resp), response);
    }

}
